<?php
//领券人相关

namespace App\Http\Controllers\Manage;

use App\Http\Controllers\BaseController;
use App\Services\Manage\ReceiverImportService;
use App\Services\Manage\ReceiverManageService;
use Carbon\Carbon;
use Carbon\CarbonImmutable;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Storage;

class ReceiverController extends BaseController
{

    /**
     * 领券人列表
     * @param  Request  $request
     * @return JsonResponse
     */
    public function list(Request $request)
    {
        $page = $request->get('page', 1);
        $pageSize = $request->get('page_size', 1);

        $res = ReceiverManageService::list([
            'activity_id'     => (int) $request->get('activity_id', 0),
            'name_keyword'    => $request->get('name_keyword', ''),
            'gender'          => $request->get('gender', 0), //性别
            'age'             => (int) $request->get('age', ''), //年龄
            'id_number'       => $request->get('id_number', ''),
            'mobile'          => (int) $request->get('mobile', ''),
            'real_id'         => (int) $request->get('real_id', 0), //编号，受助人唯一标识
            'receiver_status' => (int) $request->get('receiver_status', 0), //是否有资格领取： 1有资格，2没资格
            'auth_type'       => (int) $request->get('auth_type', 0), //是否认证：1.未认证；2.自主认证；3.协助认证；4.认证过期；
        ], $page, $pageSize);

        return $this->success($res);
    }

    /**
     * 导出列表
     * @param  Request  $request
     * @return JsonResponse
     */
    public function exportList(Request $request)
    {
        $activityId = $request->get('activity_id', 0);
        $verify = $request->get('verify', 0);
        if ($verify != 'T6541Uk73H') {
            return $this->error(1001, '参数错误');
        }

        $res = ReceiverManageService::exportList($activityId);

        return $this->success($res);
    }


    /**
     * 领券人导入校验
     * @param  Request  $request
     * @return JsonResponse
     */
    public function importReceiverVerify(Request $request)
    {
        $file = $request->file('file');
        $activityId = $request->post('activity_id');

        $res = ReceiverManageService::importReceiverVerify($activityId, $file);

        if ($res['errcode']) {
            return $this->error($res['errcode'], $res['errmsg']);
        }

        return $this->success($res['data']);
    }

    /**
     * 领券人导入
     * @param  Request  $request
     * @return JsonResponse
     */
    public function importReceiver(Request $request)
    {
        $file = $request->file('file');
        $activityId = $request->post('activity_id');

        $res = ReceiverManageService::importReceiverFile($activityId, $file);

        if ($res['errcode']) {
            return $this->error($res['errcode'], $res['errmsg']);
        }

        return $this->success([], '导入成功');
    }

    /**
     * 领券人导入 公开
     * @param  Request  $request
     * @return JsonResponse
     */
    public function importReceiverOpen(Request $request)
    {
        $file = $request->file('file');
        $activityId = $request->post('activity_id');

        $res = ReceiverImportService::importReceiverFile($activityId, $file);

        if ($res['errcode']) {
            return $this->error($res['errcode'], $res['errmsg']);
        }

        return $this->success();
    }

    /**
     * 删除领券人
     * @param  Request  $request
     * @return JsonResponse
     */
    public function del(Request $request)
    {
        $receiverId = $request->get('receiver_id');

        $res = ReceiverManageService::delReceiver($receiverId);
        if ($res['errcode']) {
            return $this->error($res['errcode'], $res['errmsg']);
        }

        return $this->success([], $res['errmsg']);
    }

    /**
     * 获取领券人基本信息
     * @param  Request  $request
     * @return JsonResponse
     */
    public function baseInfo(Request $request)
    {
        $receiverId = $request->get('receiver_id');

        $res = ReceiverManageService::getBaseInfo($receiverId);

        return $this->success($res);
    }

    /**
     * 修改受助人领取资格
     * @param  Request  $request
     * @return JsonResponse
     */
    public function updateReceiverStatus(Request $request)
    {
        $receiverId = $request->post('receiver_id');
        $status = $request->post('status'); //1.认证；2.取消认证
        $remark = $request->post('remark', '');

        $res = ReceiverManageService::updateReceiverStatus($receiverId, $status, $remark);
        if ($res['errcode']) {
            return $this->error($res['errcode'], $res['errmsg']);
        }

        return $this->success([], $res['errmsg']);
    }

    /**
     * 受助人领取优惠券列表
     * @param  Request  $request
     * @return JsonResponse
     */
    public function couponList(Request $request)
    {
        $receiverId = $request->get('receiver_id');
        $page = $request->get('page', 1);
        $pageSize = $request->get('page_size', 20);

        $res = ReceiverManageService::getCouponList($receiverId, $page, $pageSize);

        return $this->success($res);
    }

    /**
     * 受助人导入模版url
     * @param  Request  $request
     * @return JsonResponse
     */
    public function importTemplateUrl(Request $request)
    {
        $url = 'https://outsource-public-read.tos-cn-beijing.volces.com/csaf-elderlymeal/uploads/template/受助人导入模版.xlsx';

        return $this->success(['url' => $url]);
    }

}
